package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/models/subject"
	"vrcm/src/vrcm/common/apis"

	"vrcm/src/vrcm/app/admin/service/dto/subject"
	"vrcm/src/vrcm/app/admin/service/subject"
	"vrcm/src/vrcm/common/actions"
)

type Paper struct {
	apis.Api
}

// GetPage 获取Paper列表
// @Summary 获取Paper列表
// @Description 获取Paper列表
// @Tags Paper
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Paper}} "{"code": 200, "data": [...]}"
// @Router /api/v1/paper [get]
// @Security Bearer
func (e Paper) GetPage(c *gin.Context) {
	req := dto.PaperGetPageReq{}
	s := service.Paper{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	req.IsDelete = 2
	req.PaperType = 2
	list := make([]subject.Paper, 0)
	var count int64

	err = s.GetPage(&req, nil, &list, &count)
	if err != nil {
		e.Error(10001, err, fmt.Sprintf("获取Paper失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Paper
// @Summary 获取Paper
// @Description 获取Paper
// @Tags Paper
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Paper} "{"code": 200, "data": [...]}"
// @Router /api/v1/paper/{id} [get]
// @Security Bearer
func (e Paper) Get(c *gin.Context) {
	req := dto.PaperGetReq{}
	s := service.Paper{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object subject.Paper

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Paper失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建Paper
// @Summary 创建Paper
// @Description 创建Paper
// @Tags Paper
// @Accept application/json
// @Product application/json
// @Param data body dto.PaperInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/paper [post]
// @Security Bearer
func (e Paper) Insert(c *gin.Context) {
	req := dto.PaperInsertReq{}
	s := service.Paper{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建Paper失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改Paper
// @Summary 修改Paper
// @Description 修改Paper
// @Tags Paper
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.PaperUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/paper/{id} [put]
// @Security Bearer
func (e Paper) Update(c *gin.Context) {
	req := dto.PaperUpdateReq{}
	s := service.Paper{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改Paper失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Paper
// @Summary 删除Paper
// @Description 删除Paper
// @Tags Paper
// @Param data body dto.PaperDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/paper [delete]
// @Security Bearer
func (e Paper) Delete(c *gin.Context) {
	s := service.Paper{}
	req := dto.PaperDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除Paper失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}
